package com.example.customcomposedemo.customview

import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

/**
 *@author: njb
 *@date:  2023/4/5 23:49
 *@desc:
 */
@Composable
fun StickyHeader() {
    val scrollState = rememberScrollState()

    Column(
        modifier = Modifier.verticalScroll(scrollState),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        repeat(20) {
            Spacer(modifier = Modifier.height(100.dp))
            Text(
                text = "Item #$it",
                fontSize = 24.sp,
                modifier = Modifier.width(300.dp).height(100.dp)
            )
        }
    }

    val titleState = if (scrollState.value > 0) {
        ScrollStatus.Sticky
    } else {
        ScrollStatus.Inline
    }

    Title(titleState)
}

@Composable
fun Title(scrollStatus: ScrollStatus) {
    Column(
        modifier = Modifier
            .height(50.dp)
            .fillMaxWidth()
            .background(color = Color.White)
            .let {
                when (scrollStatus) {
                    ScrollStatus.Inline -> it
                    ScrollStatus.Sticky -> it
                        .padding(top = 24.dp, bottom = 8.dp)
                        .border(
                            BorderStroke(0.5.dp, Color.Gray),
                            RoundedCornerShape(0.dp, 0.dp, 8.dp, 8.dp)
                        )
                        .offset(y = (-24).dp)
                }
            },
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            text = "Title",
            fontSize = 18.sp,
            fontWeight = FontWeight.Bold
        )
    }
}

enum class ScrollStatus {
    Inline,
    Sticky
}